<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />

    <link
      rel="stylesheet"
      href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"
    />
    <link rel="stylesheet" href="../lib/d3-flamegraph.css" />

    <style>
      /* Space out content a bit */
      body {
        padding-top: 20px;
        padding-bottom: 20px;
      }

      /* Custom page header */
      .header {
        padding-bottom: 20px;
        padding-right: 15px;
        padding-left: 15px;
        border-bottom: 1px solid #e5e5e5;
      }

      /* Make the masthead heading the same height as the navigation */
      .header h3 {
        margin-top: 0;
        margin-bottom: 0;
        line-height: 40px;
      }

      /* Customize container */
      .container {
        max-width: 990px;
      }
    </style>

    <title>Performance Measurements</title>

    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
  </head>
  <body>
    <div class="container">
      <div class="header clearfix">
        <nav>
          <div class="pull-right">
            <form class="form-inline" id="form">
              <a class="btn" href="javascript: resetZoom();">Reset zoom</a>
              <a class="btn" href="javascript: clear();">Clear</a>
              <div class="form-group">
                <input type="text" class="form-control" id="term" />
              </div>
              <a class="btn btn-primary" href="javascript: search();">Search</a>
            </form>
          </div>
        </nav>
        <h3 class="text-muted">d3-flame-graph</h3>
      </div>
      <div id="chart"></div>
      <hr />
      <div id="details"></div>
    </div>

    <!-- D3.js -->
    <script src="https://d3js.org/d3.v7.js"></script>

    <!-- d3-flamegraph -->
    <script src="../lib/d3-flamegraph.js"></script>
    <script src="../lib/d3-flamegraph-tooltip.js"></script>

    <script>
      var chart = flamegraph()
        .width(960)
        .cellHeight(18)
        .transitionDuration(750)
        .minFrameSize(5)
        .transitionEase(d3.easeCubic)
        .sort(true)
        //Example to sort in reverse order
        //.sort(function(a,b){ return d3.descending(a.name, b.name);})
        .title('')
        .onClick(onClick)
        .selfValue(false)
        .setColorMapper((d, originalColor) =>
          d.highlight ? '#6aff8f' : originalColor,
        );

      // Example on how to use custom a tooltip.
      var tip = flamegraph.tooltip
        .defaultFlamegraphTooltip()
        .text((d) => 'name: ' + d.data.name + ', value: ' + d.data.value);
      chart.tooltip(tip);

      var details = document.getElementById('details');
      chart.setDetailsElement(details);

      // Example on how to use searchById() function in flamegraph.
      // To invoke this function after loading the graph itself, this function should be registered in d3 datum(data).call()
      // (See d3.json invocation in this file)
      function invokeFind() {
        var searchId = parseInt(location.hash.substring(1), 10);
        if (searchId) {
          find(searchId);
        }
      }

      // Example on how to use custom labels
      // var label = function(d) {
      //  return "name: " + d.name + ", value: " + d.value;
      // }
      // chart.label(label);

      // Example of how to set fixed chart height
      // chart.height(540);

      d3.json('stacks.json')
        .then((data) => {
          d3.select('#chart').datum(data).call(chart).call(invokeFind);
        })
        .catch((error) => {
          return console.warn(error);
        });

      document
        .getElementById('form')
        .addEventListener('submit', function (event) {
          event.preventDefault();
          search();
        });

      function search() {
        var term = document.getElementById('term').value;
        chart.search(term);
      }

      function find(id) {
        var elem = chart.findById(id);
        if (elem) {
          console.log(elem);
          chart.zoomTo(elem);
        }
      }

      function clear() {
        document.getElementById('term').value = '';
        chart.clear();
      }

      function resetZoom() {
        chart.resetZoom();
      }

      function onClick(d) {
        console.info(`Clicked on ${d.data.name}, id: "${d.id}"`);
        history.pushState({ id: d.id }, d.data.name, `#${d.id}`);
      }
    </script>
  </body>
</html>
